/**
 * Created by oureda on 6/5/16.
 */
var Session = require('./session');
var Sign = require('./sign');
module.exports = {
    user : function(req){
        if(!! req.currentUser) return req.currentUser;
        var token,user,
            isPhone = this.isPhone(req);
        token = isPhone ? req.get('token') : req.cookies.token;
        if(token == null || typeof(token) === 'undefined') return null;
        user = Session.get(token);
        return user;
    },
    login : function(user){
        var token = Sign.generatorToken(user.phone, user.pwd);
        req.currentUser = user;
        Session.setx(token, 60 * 60, user);
        return token;
    },
    isPhone : function(req){
        //return !! req.get('Agent');
        return true;
    }
};